package com.kuangstudy.controller;


import com.google.common.collect.Lists;
import com.kuangstudy.bean.User;
import com.kuangstudy.framework.anno.GetMapping;
import com.kuangstudy.framework.anno.ResponseBody;
import com.kuangstudy.framework.view.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexController {

    private Map<String, User> userDatabase = new HashMap<String, User>() {
        {
            List<User> users = Lists.newArrayList( //
                    new User("yykk@example.com", "yykk123", "yykk", "This is yykk."),
                    new User("Harry@example.com", "Harrycat", "Tom", "This is Harry."));
            users.forEach(user -> {
                put(user.email, user);
            });
        }
    };

    @GetMapping("/")
    public ModelAndView index(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return new ModelAndView("/index.html", "user", user);
    }

    @GetMapping("/hello")
    public ModelAndView hello(String name) {
        if (name == null) {
            name = "World";
        }
        return new ModelAndView("/hello.html", "name", name);
    }

    @GetMapping("/getuser")
    @ResponseBody
    public User getUser(String id) {
        System.out.println("---id----->"+id);
        return new User("yykk@example.com", "yykk123", "yykk", "This is yykk.");
    }
}
